package com.example.tests;

import java.util.Random;

import org.testng.annotations.Test;

import com.example.fw.Folders;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

public class TestFolderCreation extends TestBase {
	
	@Test
	public void testFolderCreation() {
		String folder = "newfolder " + generateRandomString();
		Folders oldFolders = app.getFolderHelper().getFolders();
		assertThat(app.getFolderHelper().createFolder(folder), is(nullValue()));
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.withAdded(folder)));

	}

	@Test
	public void testVariousFoldersCreation() {
		String folder1 = "newfolder1 " + generateRandomString();
		String folder2 = "newfolder2 " + generateRandomString();
		Folders oldFolders = app.getFolderHelper().getFolders();
		assertThat(app.getFolderHelper().createFolder(folder1), is(nullValue()));
		Folders newFolders = app.getFolderHelper().getFolders();
	    assertThat(newFolders, equalTo(oldFolders.withAdded(folder1)));
		assertThat(app.getFolderHelper().createFolder(folder2), is(nullValue()));
		Folders newFolders2 = app.getFolderHelper().getFolders();
		assertThat(newFolders2, equalTo(newFolders.withAdded(folder2)));
		
	}
	
	@Test
	public void testFoldersWithSameNameCreation() {
		String folder3 = "newfolder3 " + generateRandomString();
		Folders oldFolders = app.getFolderHelper().getFolders();
		assertThat(app.getFolderHelper().createFolder(folder3), is(nullValue()));
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.withAdded(folder3)));
		assertThat(app.getFolderHelper().createFolder(folder3), containsString("Duplicated folder name"));
		Folders newFolders2 = app.getFolderHelper().getFolders();
		assertThat(newFolders2, equalTo(newFolders));
		
	}
	
	private String generateRandomString() {
		Random rnd = new Random();
		return "" + rnd.nextInt(1000);
	}
}